import { deleteRow } from '@/api/content'
import callback from '@/builder/mixins/callback'

export default {
  name: 'AdminTableDelete',
  mixins: [callback],
  render(h) {
    const callback = this.callbackComponent(h)

    return h('el-button', {
      props: {
        size: this.size,
        type: 'danger',
      },
      on: {
        click: this.onDelete,
      },
    }, ['删除', callback])
  },
  props: {
    action: {
      type: String,
      required: true,
    },
    size: String,
    data: {
      type: Array,
      default: () => [],
    },
    row: {
      type: Object,
      default: () => ({}),
    },
    index: Number,
  },
  methods: {
    onDelete() {
      this
        .$confirm('删除后不可恢复，确定删除？', '删除确认', {
          type: 'warning',
        })
        .then(() => {
          deleteRow(this.action)
            .then((res) => {
              const data = res.data
              if (data.status) {
                data.callback && this.callback(data.callback)
                this.$nextTick(() => {
                  this.data.splice(this.index, 1)
                })
              }
            })
        })
        .catch(() => {})
    },
  },
}
